<template>
  <div class="dialog">
    <el-dialog
      :title="titleText"
      :visible.sync="showDialog"
      width="45%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="模板名称:" prop="jobTemplateName" class="w100p">
              <el-input
                v-model="formData.jobTemplateName"
                placeholder="请输入模板名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="关联工序:"
              prop="productionProcessesNo"
              class="w100p"
            >
              <el-select
                v-model="formData.productionProcessesNo"
                placeholder="请选择"
                clearable
                @change="changePdtProcess"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否有工艺参数:" prop="existing" class="w100p">
              <el-select
                v-model="formData.existing"
                placeholder="请选择"
                clearable
                @change="existChange"
              >
                <el-option
                  v-for="item in existingOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <template v-if="formData.existing">
          <el-form
            v-for="(craftItem,idx) in formData.craftList"
            :key="idx"
            label-position="left"
            :model="craftItem"
            class="w100p formChildren"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="工艺参数来源:" prop="source" class="w100p">
                  <el-select
                    v-model="craftItem.source"
                    placeholder="请选择"
                    clearable
                  >
                    <el-option
                      v-for="item in sourceOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col v-if="craftItem.source === 0" :span="12">
                <el-form-item label="选择设备:" prop="equipmentCode" class="w100p">
                  <el-select
                    v-model="craftItem.equipmentCode"
                    placeholder="请选择"
                    clearable
                    @change="changeEquip(craftItem.equipmentCode)"
                  >
                    <el-option
                      v-for="item in equipOptions"
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="craftItem.source === 0" :span="12">
                <el-form-item label="选择设备数据:" prop="dataItemName" class="w100p">
                  <el-select
                    v-model="craftItem.dataItemName"
                    placeholder="请选择"
                    clearable
                    @change="getcraftItem($event,idx)"
                  >
                    <el-option
                      v-for="item in dataItemIdOptions"
                      :key="item.value"
                      :label="item.name"
                      :value="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="craftItem.source === 1" :span="12">
                <el-form-item label="工艺参数名称:" prop="craftName" class="w100p">
                  <el-input
                    v-model="craftItem.craftName"
                    clearable
                    placeholder="请输入工艺参数名称"
                  />
                </el-form-item>
              </el-col>
              <el-col v-if="craftItem.source === 1" :span="12">
                <el-form-item label="单位" prop="unitNo" class="w100p">
                  <el-select
                    v-model="craftItem.unitNo"
                    placeholder="请选择单位"
                    clearable
                  >
                    <el-option
                      v-for="item in unitOptions"
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-button @click="addCraftList">添加工艺参数</el-button>
          <el-button @click="delCraftList">删除工艺参数</el-button>
        </template>
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择作业人:" prop="personNo" class="w100p">
              <el-select
                v-model="formData.personNo"
                placeholder="请选择作业人"
                clearable
              >
                <el-option
                  v-for="item in personOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from "@/utils/index";
export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    options: {
      type: Array,
      default: () => []
    },
    personOptions: {
      type: Array,
      default: () => []
    },
    equipOptions: {
      type: Array,
      default: () => []
    },
    unitOptions: {
      type: Array,
      default: () => []
    },
    dataItemIdOptions: {
      type: Array,
      default: () => []
    },
    editData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // 选择的设备型号
      equipCode: "",
      showDialog: this.show,
      // 展示
      formData: {
        craftList: [
          {
            source: "",
            dataItemId: "",
            craftName: "",
            unitNo: ""
          }
        ]
      },
      existingOptions: [
        {
          value: false,
          label: "否"
        },
        {
          value: true,
          label: "是"
        }
      ],
      sourceOptions: [
        {
          value: 0,
          label: "设备数据采集"
        },
        {
          value: 1,
          label: "人工手动录入"
        }
      ],
      rules: {
        jobTemplateName: [
          {
            required: true,
            message: "请输入模板名称",
            trigger: ["change", "blur"]
          }
        ],
        productionProcessesNo: [
          {
            required: true,
            message: "请选择关联工序",
            trigger: ["change", "input", "blur"]
          }
        ],
        existing: [
          {
            required: true,
            message: "请选择是否有工艺参数",
            trigger: ["change", "blur"]
          }
        ],
        personNo: [
          {
            required: true,
            message: "请选择作业人",
            trigger: ["change", "blur"]
          }
        ]
      }
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    editData(v) {
      this.formData = deepClone(v);
    }
  },
  created() {},
  mounted() {},
  methods: {
    getcraftItem(e, idx) {
      console.log(e, this.dataItemIdOptions, idx);
      this.dataItemIdOptions.forEach(item => {
        if (item.name === e) {
          this.formData.craftList[idx].dataItemId = item.value;
          this.formData.craftList[idx].craftName = e;
        }
      });
      console.log(this.formData);
    },
    // 添加工艺参数
    addCraftList() {
      this.formData.craftList.push({
        source: "",
        dataItemId: "",
        craftName: "",
        unitNo: ""
      });
    },
    // 删除工艺参数
    delCraftList() {
      if (this.formData.craftList.length === 1) {
        this.$message.error("已经是最少工艺参数了");
        return;
      }
      this.formData.craftList.pop();
    },
    // 改变设备类型
    changePdtProcess() {
      this.$emit("changePdtProcess", this.formData.productionProcessesNo);
    },
    changeEquip(val) {
      this.$emit("changeEquip", val);
    },
    existChange() {
      if (!this.formData.existing) {
        this.formData.craftList = [];
      }
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          console.log(this.formData);
          // 前端获取一下人物名字
          if (this.formData.personNo) {
            this.personOptions.forEach(item => {
              if (item.value === this.formData.personNo) {
                this.formData.personName = item.name;
              }
            });
          }
          this.$emit("addEvent", this.formData);
          this.close();
        }
      });
    },
    close() {
      this.$emit("closeDialog");
      this.resetForm("ruleForm");
    },
    cancel() {
      this.$emit("closeDialog");
      this.resetForm("ruleForm");
    },
    // 重置验证
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

  <style lang="scss" scoped>
::v-deep
  .el-dialog__wrapper
  .el-dialog
  .el-dialog__body
  .el-form
  .el-form-item
  .el-form-item__label {
  min-width: 160px !important;
}
.formChildren {
  border: 1px dashed #0ea8c4;
  padding-top: 12px;
  margin-bottom: 12px;
}
</style>
